<?php
/**
 * [DB模型]
 * 
 * @version $Id: User.php 297 2012-05-08 01:54:09Z linsir123 $
 */
class dbUser extends ActiveRelation 
{
	/**
	 * @var 数据库表名
	 */
	public $table = 'user';
	
	/**
	 * 取得列表
	 */
	public function getList($start, $offset, $params = array())
	{
		$this->_searchSql($params);
		
		///
		$this->limit($start, $offset);
		return $this->getResults();
	}
	
	/**
	 * 取得总数
	 */
	public function getListCount($params = array())
	{
		$this->_searchSql($params);
		
		///
		return $this->getCount();
	}
	
	/**
	 * 构造查询SQL
	 */
	private function _searchSql($params)
	{
		$db = D::account();
		$db2 = D::profile();
		$db->field('balance,state');
		$db2->field('alipay');
		
		///
		if ( ! empty($params['uid'])) 
			$this->where('id', $params['uid']);
			
		if (isset($params['type'])) 
			$this->where('type', $params['type']);
			
		if (isset($params['state'])) 
			$db->where('state', $params['state']);
			
		if (isset($params['order'])) {
			list($field, $desc) = $params['order'];
			if ($field == 'balance') 
				$db->order("{$field} {$desc}");
			else 
				$this->order("{$field} {$desc}");
		} else 
			$this->order("create_time desc");
			
		///
		$this->joinLeft($db, 'id', 'uid');
		$this->joinLeft($db2, 'id', 'uid');
	}
	
	/**
	 * 取得一个
	 */
	public function getByOauth($type, $key)
	{
		$this->where(array('type'=>$type, 'oauth_key'=>$key));
		return $this->getRow();
	}
}
